package com.shuziren.controller.QuestionController;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shuziren.abnormal.CustomException;
import com.shuziren.question.entity.ProblemsetTopic;
import com.shuziren.question.entity.dto.InterviewQuestionSetDTO;
import com.shuziren.question.entity.InterviewQuestionSet;
import com.shuziren.core.vo.Result;
import com.shuziren.question.entity.dto.UpdateInterviewQuestionSet;
import com.shuziren.question.service.InterviewQuestionSetService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理题目集相关的HTTP请求
 * @author 123
 */
@RestController
@RequestMapping("/api/questionSet")
public class InterviewQuestionSetController {

    @Autowired
    private InterviewQuestionSetService interviewQuestionSetService;

    /**
     * 查询题目集下的题目
     * @param id
     * @return
     */
    @GetMapping("/selectProblemsetTopic/{id}")
    public Result<List<ProblemsetTopic>> selectProblemsetTopic(@PathVariable("id") Integer id) {
        List<ProblemsetTopic> problemsetTopics = interviewQuestionSetService.selectProblemsetTopic(id);
        return problemsetTopics != null ? Result.success(problemsetTopics) : Result.fail("未找到该题目集下的题目");
    }
    /**
     * 添加面试题目集
     * @param questionSetDTO
     * @return
     */
    @PostMapping("/add")
    public Result<String> addQuestionSet(@RequestBody InterviewQuestionSetDTO questionSetDTO) {
        String result = null;
        try {
            result = interviewQuestionSetService.addQuestionSet(questionSetDTO);
        } catch (CustomException e) {
            throw new RuntimeException(e);
        }

        if ("添加成功".equals(result)) {
            return Result.success(result);
        } else if ("题目集创建成功，但未添加题目".equals(result)) {
            return Result.success("添加成功，但未添加题目");
        } else {
            return Result.fail("添加失败");
        }
    }

    /**
     * 查询面试题目集
     *
     * @param querySet
     * @return 返回查询结果
     */
    //return interviewQuestionSetService.selectQuestionSet(querySet);
    @PostMapping("/list")
    public IPage<InterviewQuestionSet> getQuestionSetList(@RequestBody InterviewQuestionSetDTO querySet) {
        return interviewQuestionSetService.selectQuestionSet(querySet);
    }


    // 更新面试题目集return ResponseEntity.ok(interviewQuestionSetService.updateById(questionSet));
    @PutMapping("/update/{id}")
    public Result<String> updateQuestionSet(
            @PathVariable("id") Integer id,
            @RequestBody @Validated UpdateInterviewQuestionSet questionSet) {
        if (id == null || id <= 0) {
            return Result.fail("id不能为空");
        }

        questionSet.setId(id);
        InterviewQuestionSet questionSet1 = new InterviewQuestionSet();
        BeanUtils.copyProperties(questionSet, questionSet1);
        questionSet1.setId(id);

        boolean b = interviewQuestionSetService.updateQuestionSet(questionSet1);
        if (!b){
            throw new RuntimeException("更新失败");
        }
        return Result.success("修改成功");
    }

    /**
     * 删除面试题目集
     *
     * @param id
     * @return 删除成功返回1，删除失败返回0，异常返回-1
     */
    @GetMapping("/delete/{id}")
    public Result<String> removeQuestionSet(@PathVariable("id") Integer id) {
        /*try {*/
        int result = 0;
        try {
            result = interviewQuestionSetService.removeQuestionSet(id);
        } catch (CustomException e) {
            throw new RuntimeException(e);
        }
        if (result > 0) {
            // 200
            return Result.success("删除成功");
        }
        /*else {
                //
                return Result.failer();
            }
        } catch (RuntimeException e) {
            // 400 异常
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        }*/
        return Result.fail("删除失败");
    }

} 